package com.spark.controller;

import com.spark.Service.UserService;
import com.spark.pojo.Msg;
import com.spark.pojo.User;
import com.spark.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;

/**
 * @author zhouqichun
 * @version 1.0
 * @date 2021/8/15 - 08 - 15 - 10:15
 * @description diary com.spark.controller
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response){
        User user = userService.getLoginUser(username, password);
        if (user != null){
            Cookie cookie = new Cookie(Constants.USER_COOKIE,user.getId());
            cookie.setMaxAge(60*60);
            response.addCookie(cookie);
            return "redirect:/html/home.html";
        }
        return "login";
    }

    @PostMapping("/register")
    public String register(@RequestParam("username")String username, @RequestParam("pwd")String password,
                           @RequestParam("mail")String email, @RequestParam("gender")int gender){
        User user = new User(UUID.randomUUID().toString(), username, password, gender, new Date(),
                Constants.DEFAULT_NOTE, Constants.DEFAULT_PICTURE_ADDRESS, email);
        int result = userService.addUser(user);
        if (result>0){
            return "redirect:/html/login.html";
        }
        return "register";
    }
    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(Constants.USER_COOKIE)){
                cookie.setMaxAge(0);
            }
        }
        return "login";
    }

    @ResponseBody
    @RequestMapping("/getUser/{userId}")
    public Msg getUserById(@PathVariable("userId")int id){
        User user = userService.getUserById(id);
        return Msg.success().addAttribute("user",user);
    }
    @RequestMapping("/updateUser")
    public String updateUser(String name,String pwd,String note, HttpServletRequest request){
        User user = new User();
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals(Constants.USER_COOKIE)){
                user.setId(cookie.getValue());
                cookie.setMaxAge(0);
                break;
            }
        }
        user.setUserName(name);
        user.setNote(note);
        user.setPwd(pwd);
        userService.updateUser(user);
        return "login";
    }
}
